/**
 * 
 */
package org.lookouster.mopg.connector;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;

/**
 * @author lkster
 *
 */
public class Database {
	
	static Connection conn;

	/**
	 * Called at launch
	 */
	public static boolean connect()
	{
		final String url = "jdbc:mysql://localhost:3306/mopg";
	    try {
			conn = (Connection) DriverManager.getConnection(url,"root","");
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
	/**
	 * Used for executing select queries
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public static ResultSet retrieveData(String query)
	{
		try {
			ResultSet rs = conn.createStatement().executeQuery(query);
			rs.next();
			return rs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Used for executing other queries that don't imply
	 * a result set return.
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public static Boolean execute(String query)
	{
		try {
				conn.createStatement().execute(query);
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * To be called at the app exit
	 * @return
	 */
	public static Boolean disconnect()
	{
		try {
			conn.close();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	
	
}
